<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="教室名称">
              <a-input v-model="queryParam.classroomNo"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="所在校区">
              <a-select allowClear v-model="queryParam.jsxq">
              <a-select-option v-for="i in selectMenu.campusList" :key="i.id" :value="i.xqbh">{{ i.xqmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="所属院系">
                <a-select allowClear :disabled="selectMenu.yxList.length === 1"  v-model="queryParam.deptCode" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="是否可用">
                <a-select allowClear v-model="queryParam.isUsable">
                  <a-select-option value="1">可用</a-select-option>
                  <a-select-option value="0">不可用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="教室类型">
                <a-select allowClear v-model="queryParam.typeCode">
                  <a-select-option v-for="i in TYPE0046" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            
          </template>
          <a-col :md="10" :sm="20">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="toAdd" style="margin:0 10px">添加</a-button>
            <a-button type="primary" @click="batchDelete" style="margin:0 10px">删除</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{x: 1200}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="isUsable" slot-scope="text">
          {{ +text === 1 ? '可用' : '不可用' }}
        </span>
        <a slot="classroom" slot-scope="a, b" @click="showDetail(b.id)">{{a}}</a>
        <template slot="operation" slot-scope="id">
          <a @click="showEdit(id)">修改</a>
        </template>
      </s-table>
    </a-card>

    <info-modal ref="info" :editId="editId" :visible="infoVisible" @cancel="infoVisible = false" @refresh="$refs.table.refresh(false)"/>
  </div>
</template>

<script>
import { STable } from '~'
import infoModal from './infoModal'
import searchMixin from '@/utils/searchRemain'
import { mapState } from 'vuex'

export default {
  mixins: [ searchMixin ],
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '所属院系',
          dataIndex: 'deptName'
        },
        {
          title: '教室名称',
          dataIndex: 'classroomNo',
          scopedSlots: { customRender: 'classroom' }
        },
        {
          title: '位置',
          dataIndex: 'location'
        },
        {
          title: '是否可用',
          dataIndex: 'isUsable',
          scopedSlots: { customRender: 'isUsable' }
        },
        {
          title: '容量',
          dataIndex: 'capacity'
        },
        {
          title: '教室类型',
          dataIndex: 'typeCodeName'
        },
        {
          title: '排课优先级',
          dataIndex: 'priority'
        },
        {
          title: '备注',
          dataIndex: 'note'
        },
        {
          title: '操作',
          dataIndex: 'id',
          scopedSlots: { customRender: 'operation' },
          width:80,
          fixed:'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.classrommList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yxList: []
      },
      infoVisible: false,
      confirmLoading: false,
      editId: null,
      initDone: false
    }
  },
  computed: {
    ...mapState({
      TYPE0046: state => state.app['TYPE0046'],
    })
  },
  components: {STable, infoModal},
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0046')
    // this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$api.base.campusAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data,
          campusList: res[1].data
        }
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'deptCode', this.selectMenu.yxList[0].dwh)
        }
        this.initDone = true
      })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的教室')
        return
      }
      this.$confirm({
        title: '确定删除所选中的教室?',
        content: '当您点击确定按钮后，这些记录将会被删除',
        centered: true,
        onOk: () => {
          let ids = this.selectedRowKeys.join(',')
          this.$api.cultivate.classroomDelete(ids).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    showEdit (id) {
      this.$api.cultivate.classroomInfo(id).then(res => {
        this.$refs.info.fillForm(res.data)
      })
      this.infoVisible = true
      this.$refs.info.showOnly = false
      this.editId = id
    },
    showDetail (id) {
      this.$api.cultivate.classroomInfo(id).then(res => {
        this.$refs.info.fillForm(res.data)
        this.$refs.info.showOnly = true
      })
      this.infoVisible = true
      this.editId = id
    },
    toAdd () {
      this.$refs.info.showOnly = false
      this.infoVisible = true
      this.editId = null
      this.$refs.info.form.resetFields()
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    }
  }
}
</script>

<style>

</style>
